# -*- coding: utf-8 -*-
# @Time    : 2022/7/28 14:21
# @Author  : Yanfeng
# @Email   : sxyanfeng2014@163.com
# @File    : functions_control.py
# @Software: PyCharm
import hashlib
import uuid as uid
import datetime
import random
import re
import string

def md5(arg):
    '''
    md5加密
    :param arg:目标字符串
    :return: 加密后的字符串
    '''
    hash = hashlib.md5()
    hash.update(arg.encode("utf-8"))
    return hash.hexdigest()


def uuid():
    '''
    生成 UUID
    :return:
    '''
    return uid.uuid1()

def randomint(length):
    '''
    生成指定长度随机数字
    :param length:
    :return:
    '''
    s = [str(i) for i in range(10)]
    return ''.join(random.sample(s, length))

def randomstr(length):
    '''
    生成指定长度随机数字和大小写字母组合
    :param length:
    :return:
    '''
    return ''.join(random.sample(string.ascii_letters + string.digits+ string.digits, length))

def now(pattern="%Y-%m-%d %H:%M:%S", hours=0):
    '''
    生成当前时间 可格式化和设置时间偏移
    :param pattern: 格式如  %Y-%m-%d %H:%M:%S
    :param hours: 设置小时偏移量  如 hours=1 代表当前时间加一小时，支持负数
    :return:
    '''

    return (datetime.datetime.now() + datetime.timedelta(hours=hours)).strftime(pattern)


def regex(target_str, pattern, index=0):
    '''
    正则匹配
    :param target_str: 目标字符串
    :param parttern: 正则表达式
    :param index: 列表索引
    :return: 所有匹配结果，列表形式
    '''
    results = re.findall(pattern, target_str)
    return results[index] if results != [] else results


if __name__ == '__main__':
    print(now())
    print(randomstr(10))
    print(randomint(5),type(randomint(5)))
    print(uuid())
    print(md5('woshiyanfeng'))
    print(regex('aabbabaabbaa', 'a.b'))
    a = random.choice("qwqesadad231312312")
    print(a)

    items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    items.sort(reverse=True)
    print(items)
    print(random.random())
    print(random.uniform(1, 10))
    print(random.randrange(1, 101, 2))
    print(random.randint(100, 10000))
    print("".join(random.sample(string.ascii_letters + string.digits, 10)))